package com.ttn.student.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ttn.common.core.entity.Result;
import com.ttn.common.core.entity.studentrecord.LikeRecord;
import com.ttn.student.service.LikeRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * (like_record)表控制层
 *
 * @author xxxxx
 */
@RestController
@RequestMapping("/like-record")
@Slf4j
public class LikeRecordController {
    /**
     * 服务对象
     */
    @Resource
    private LikeRecordService likeRecordService;

    @PostMapping("/save/like-record")
    public Result saveLikeRecord(HttpServletRequest request, @RequestBody LikeRecord likeRecord) {
        log.info("LikeRecordController saveOralPracticeStudyRecordDetails ,参数:{}", likeRecord);
        LambdaQueryWrapper<LikeRecord> likeRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
        likeRecordLambdaQueryWrapper.eq(LikeRecord::getStudyRecordId, likeRecord.getStudyRecordId());
        likeRecordLambdaQueryWrapper.eq(LikeRecord::getLikerId, likeRecord.getLikerId());
        LikeRecord likeRecord1 = likeRecordService.getOne(likeRecordLambdaQueryWrapper);
        if (Objects.nonNull(likeRecord1)) {
            likeRecordService.removeById(likeRecord1.getId());
        } else {
            likeRecordService.save(likeRecord);
        }
        return Result.success();
    }
}
